<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>选择商品</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../../include/commonFile.jsp" %>
    <style type="text/css">
        .index-outside > section .section-main {
            min-width: 0px !important;
            padding: 0px 15px;
        }

        .index-outside > section {
            padding-bottom: 0px !important;
        }

        .layui-form-radio {
            height: 30px !important;
        }

        .expand .icon-right {
            -webkit-transform: rotate(90deg);
            -moz-transform: rotate(90deg);
            -ms-transform: rotate(90deg);
            transform: rotate(90deg);
        }

        .icon-right {
            display: inline-block;
            -webkit-transition: all .5s;
        }
    </style>
</head>
<body>
<!--头部-->
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <form class="layui-form" action="${ctx}/mall/goods/selectCategoryGoods.do" id="myForm" method="post">
                <input type="hidden" name="pageNo" id="pageNo" value="${page.page}"/>
                <div class="f-search-bar">
                    <div class="search-container">
                        <ul class="search-form-content">
                            <li class="form-item-inline"><label class="search-form-lable">商品名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input"
                                           value="${mallGoods.name}" placeholder="商品名称">
                                </div>
                            </li>
                            <li class="form-item-inline">
                                <div class="sub-btns">
                                    <a class="layui-btn layui-btn-danger"
                                       href="javascript:submitFunction('#myForm')">查询</a>
                                    <a class="layui-btn layui-btn-normal"
                                       href="javascript:resetFunction('#myForm')">重置</a>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </form>
            <div class="c-time-list-content" style="padding-top: 0px;">
                <input type="hidden" id="categoryIds"/>
                <input type="hidden" id="categoryNames"/>
                <input type="hidden" id="selectDatas"/>
                <table class="layui-table layui-form" lay-skin="line">
                    <colgroup>
                        <col>
                        <col width="150">
                        <%--<col width="150">--%>
                    </colgroup>
                    <thead>
                    <tr>
                        <th>分类名称</th>
                        <th>商品数量</th>
                        <%--<th>创建时间</th>--%>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach var="category" items="${list}">
                        <tr data-id="${category.id}">
                            <td>
                                <span>
                                    <input name="parentId" lay-filter="parentRadio" type="radio" value="${category.id}"
                                           title="${category.name}"/>
                                </span>&emsp;
                                <c:if test="${fn:length(category.mallGoodsList) > 0}">
                                    <span class="expand"><i class="iconfont icon-right"></i></span>
                                </c:if>
                            </td>
                            <td>
                                    ${fn:length(category.mallGoodsList)}
                            </td>
                            <%--<td><fmt:formatDate value="${category.createDate}" pattern="yyyy-MM-dd HH:mm"/></td>--%>
                        </tr>
                        <c:forEach var="children" items="${category.mallGoodsList}">
                            <tr class="children-tr-${category.id}" data-pName="${category.name}" data-pId="${category.id}"
                                data-name="${children.name}" data-id="${children.id}" data-pic="${children.pic}">
                                <td colspan="2">
                                    <span>
                                        <input name="id" type="checkbox" ${chk == true ? 'checked="checked"' : ''}
                                               title="${children.name}" lay-skin="primary" data-name="${children.name}"
                                               value="${children.id}" lay-filter="categoryChk"/>
                                    </span>
                                </td>
                                <%--<td><fmt:formatDate value="${children.createDate}" pattern="yyyy-MM-dd HH:mm"/></td>--%>
                            </tr>
                        </c:forEach>
                    </c:forEach>
                    </tbody>
                </table>
                <div id="page_content" class="page-container"></div>
            </div>
        </div>
    </section>
</div>
<!--底部-->
<%@include file="../../include/incScript.jsp" %>
<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript">
    $(function () {
        // 加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });
        layui.use(['form', 'laypage', 'element'], function () {
            var form = layui.form, element = layui.element;

            // 一级分类选择
            form.on('radio(parentRadio)', function (data) {
                var $target = $(data.elem);
                var parentId = $target.closest("tr").data("id");
                var checked = data.elem.checked;

                $("tbody tr[class*=children-tr]").each(function (jj, ele) {
                    $(ele).find("[type=checkbox]").prop("checked", false);
                });

                $("tr[class=children-tr-" + parentId + "]").each(function (index, ele) {
                    if (checked) {
                        $(ele).find("[type=checkbox]").prop("checked", true);
                    } else {
                        $(ele).find("[type=checkbox]").prop("checked", false);
                    }
                });
                form.render('checkbox');
            });
        });

        $("#submitBtn").click(function () {
            $("#myForm").submit();
        });

        $("#resetBtn").click(function () {
            $("#myForm input[type=text]").val("");
        });

        $("tbody").delegate(".icon-right", "click", function (e) {
            var $target = $(e.target);
            var $span = $target.parent('span');
            var parentId = $target.closest("tr").data("id");
            if ($span.hasClass("expand")) {
                $span.removeClass("expand");
                $("tr[class=children-tr-" + parentId + "]").hide();
            } else {
                $span.addClass("expand");
                $("tr[class=children-tr-" + parentId + "]").show();
            }
        })
    });

    function doSubmit() {
        var length = $("[name=id]:checked").length;
        if (length == 0) {
            util.layerMsgError("请先选择")
            return false;
        }
        var _result = new Array();
        $("[name=id]:checked").each(function (index, ele) {
            var $tr = $(ele).closest("tr");
            var map = {
                targetId: $tr.data("id"),
                name: $tr.data("name"),
                pic: $tr.data("pic"),
                pName: $tr.attr("data-pName"),
                pId : $tr.attr('data-pId'),
                type: 2
            };
            _result.push(map);
        });
        $("#selectDatas").val(JSON.stringify(_result));
        return true;
    }
</script>
</body>
</html>